<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of p
 *
 * @author Albadr
 */
class N extends CI_Controller {
    private $valid = false;
    private $doc_name = "doc.all";
    private $que_name = "query.text";
    private $rel_name = "relevant.jud";
    private $stop_name = "stop.words";
    private $queryOptions = Array();
    private $documentOptions = Array();
    
    public function experiment(){
        error_reporting(0);
        $START = time();
        /*Load Model*/
        $this->load->model("missparse");
        $this->load->model("inv_file");
        /*Upload File*/
        $this->upload();
        /*Fetch Options*/
        $this->fetchOptions();
        /*Do the hard stuff*/
        $THE_DOCUMENT = $this->missparse->docs_to_object_of_docs_clean(base_url()."upload/".$this->doc_name);
        $THE_QUERY  = $this->missparse->parse_query_to_objects_clean(base_url()."upload/".$this->que_name);
//        $STOP_WORDS;  --> di dalem
        $INVERTED_FILE = $this->inv_file->generate($THE_DOCUMENT,$this->documentOptions);
        $ALL_QUERY_WEIGHT =  $this->inv_file->computeQueryWeight($THE_QUERY,$this->queryOptions,$INVERTED_FILE);
//        $FIKRI;
        $RETRIEVED_FILE = $this->missparse->compAll($INVERTED_FILE,$ALL_QUERY_WEIGHT,sizeof($THE_DOCUMENT));
//        $RETRIEVED_FILE = $this->missparse->compute_all_sc($INVERTED_FILE,$ALL_QUERY_WEIGHT);
//        $ADI;
        $DOEXP_PROCESS = $this->missparse->calculate_performance_experiment($RETRIEVED_FILE, $this->missparse->docs_to_object_of_docs_qrel(base_url()."upload/".$this->rel_name));
        $AVG_NIAP = $this->missparse->count_average_NIAP($RETRIEVED_FILE, $this->missparse->docs_to_object_of_docs_qrel(base_url()."upload/".$this->rel_name));
        //Do Formatting For Result and Get Additional Info
        $RES=array();
        for($i = 0; $i < sizeof($DOEXP_PROCESS); ++$i){
            $query_info = $DOEXP_PROCESS[$i];
            $RES[$i]->no = $query_info["no_query"];
            $RES[$i]->query  = $THE_QUERY[$query_info["no_query"]-1]->query;
            $RES[$i]->niap  = $query_info["niap"];
            $doc=array();
            $result_query = $query_info["result_query"];
            for($j = 0; $j < sizeof($result_query); ++$j){
                $result_query_j = $result_query[$j];
                $doc[$j]->no = $result_query_j["no_document"];
                $doc[$j]->title = $THE_DOCUMENT[$result_query_j["no_document"]-1]->title;
            }
            $RES[$i]->results=$doc;
        }
        $tes = $this->missparse->docs_to_object_of_docs_qrel(base_url()."upload/".$this->rel_name);

        /*Presentation Layer*/
        echo "<pre>";
          //print_r($tes);
          //print_r($RETRIEVED_FILE);
//        print_r($INVERTED_FILE);
//        print_r($ALL_QUERY_WEIGHT);
        print_r($RES);

        echo "</pre>";
        $this->load->view('experiment',array("qresult"=>$RES,
            "avrniap"=>$AVG_NIAP,
            "time"=>$START-time()
        ));
    }
    
    public function search(){
        $START=time();
        /*Load Model*/
        $this->load->model("missparse");
        $this->load->model("inv_file");
        /*Upload File*/
        $this->upload();
        /*Fetch Options*/
        $this->fetchOptions();
        /*Do the hard stuff*/
        $THE_DOCUMENT = $this->missparse->docs_to_object_of_docs_clean(base_url()."upload/".$this->doc_name);

        $THE_QUERY->no = 1;
        $THE_QUERY->query = $_POST["query"];


        $INVERTED_FILE = $this->inv_file->generate($THE_DOCUMENT,$this->documentOptions);
        $ALL_QUERY_WEIGHT =  $this->inv_file->computeQueryWeight($THE_QUERY,$this->queryOptions,$INVERTED_FILE);
        
        //        $FIKRI;
        $RETRIEVED_FILE = $this->missparse->compAll($INVERTED_FILE,$ALL_QUERY_WEIGHT,sizeof($THE_DOCUMENT));
        //Do Formatting For Result
        $RES=array();
        for($i = 0; $i < sizeof($RETRIEVED_FILE); ++$i){
            $RES[$i]->no = $THE_DOCUMENT[$RETRIEVED_FILE[$i]["no"]-1]->no;
            $RES[$i]->title  = $THE_DOCUMENT[$RETRIEVED_FILE[$i]["no"]-1]->title;
        }

        /*Presentation Layer*/
//        echo "<pre>";
//        print_r($INVERTED_FILE);
//          print_r($ALL_QUERY_WEIGHT);
//        print_r($RES);
//    print_r($RETRIEVED_FILE);
//        echo "</pre>";
        $this->load->view('search',array(
            'results'=>$RES,
            'time'=>$START-time()
        ));
    }
    
    private function fetchOptions(){
        // ini nanti dihapus
        $qopt["tf"] = "raw";
        $qopt["idf"] = "yes";
        $qopt["norm"] = "yes";
        $qopt["stem"] = "yes";
        $qopt["stop"] = "yes";
        $this->queryOptions = $qopt;
        $dopt["tf"] = "raw";
        $dopt["idf"] = "yes";
        $dopt["norm"] = "yes";
        $dopt["stem"] = "yes";
        $dopt["stop"] = "yes";
        $this->documentOptions = $dopt;
       
//        if (!empty($_POST)) {
//            $qopt["tf"] = $_POST["qtf"];
//            $qopt["idf"] = isset($_POST["qidf"]) ? "yes":"no";
//            $qopt["norm"] = isset($_POST["qnorm"]) ? "yes":"no";
//            $qopt["stem"] = isset($_POST["stem"]) ? "yes":"no";
//            $qopt["stop"] = isset($_POST["stop"]) ? "yes":"no";
//            $this->queryOptions = $qopt;
//            $dopt["tf"] = $_POST["otf"];
//            $dopt["idf"] = isset($_POST["oidf"]) ? "yes":"no";
//            $dopt["norm"] = isset($_POST["onorm"]) ? "yes":"no";
//            $dopt["stem"] = isset($_POST["stem"]) ? "yes":"no";
//            $dopt["stop"] = isset($_POST["stop"]) ? "yes":"no";
//            $this->documentOptions = $dopt;
//        }
    }
    
    private function upload(){
        if (!empty($_FILES)) {
            if ($_FILES['docfile']['tmp_name']!=""){
                $tempFile = $_FILES['docfile']['tmp_name'];
                $targetFile = "upload/" . $this->doc_name;
                move_uploaded_file($tempFile, $targetFile);
            }
            
            if ($_FILES['queryfile']['tmp_name']!=""){
                $tempFile = $_FILES['queryfile']['tmp_name'];
                $targetFile = "upload/" . $this->que_name;
                move_uploaded_file($tempFile, $targetFile);
            }
            
            if ($_FILES['stopfile']['tmp_name']!=""){
                $tempFile = $_FILES['stopfile']['tmp_name'];
                $targetFile = "upload/" . $this->stop_name;
                move_uploaded_file($tempFile, $targetFile);
            }
            
            if ($_FILES['relfile']['tmp_name']!=""){
                $tempFile = $_FILES['relfile']['tmp_name'];
                $targetFile = "upload/" . $this->rel_name;
                move_uploaded_file($tempFile, $targetFile);
            }
        }else{
            echo "Invalid request!";
        }
    }
}

?>
